<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>字典管理</el-breadcrumb-item>
      <el-breadcrumb-item>首页菜单管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-alert title="添加修改会删除缓存，请谨慎操作" type="warning" show-icon>
      </el-alert>

      <el-row class="layoutMenu" :gutter="20">
        <el-col :span="4">
          <el-button type="primary" @click="addLayoutMenuShow"
            >添加首页菜单</el-button
          >
        </el-col>
      </el-row>

      <el-table :data="layoutMenuList" border stripe>
        <el-table-column type="index" label=""></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="priority" label="排序"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="updateGoodsTypeShow(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="delGoodsTypeById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
        title="添加首页菜单"
        :visible.sync="addDialogVisible"
        @close="addDialogclose"
        width="50%"
      >
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="70px"
        >
          <el-form-item label="分类名称:" prop="name" label-width="100px">
            <el-input v-model="addForm.name" class="inp"></el-input>
          </el-form-item>

          <el-form-item label="优先级:" prop="priority" label-width="100px">
            <el-input v-model.number="addForm.priority" class="inp"></el-input>
          </el-form-item>

          <el-form-item label="描述:" prop="description" label-width="100px">
            <el-input v-model="addForm.description" class="inp"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDialogSubmit">确 定</el-button>
        </span>
      </el-dialog>

      <el-dialog
        title="修改首页菜单"
        :visible.sync="updateDialogVisible"
        @close="updateDialogclose"
        width="50%"
      >
        <el-form
          :model="updateForm"
          :rules="updateFormRules"
          ref="updateFormRef"
          label-width="70px"
        >
          <el-form-item label="分类名称:" prop="name" label-width="100px">
            <el-input v-model="updateForm.name" class="inp"></el-input>
          </el-form-item>

          <el-form-item label="优先级:" prop="priority" label-width="100px">
            <el-input v-model.number="updateForm.priority" class="inp"></el-input>
          </el-form-item>

          <el-form-item label="描述:" prop="description" label-width="100px">
            <el-input v-model="updateForm.description" class="inp"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateDialogSubmit">确 定</el-button>
        </span>
      </el-dialog>

  </div>
</template>

<script>
import { getLayoutMenuData, addLayoutMenuData, getLayoutMenuDataById, updateLayoutMenuData, deleteLayoutMenuDataById } from '@/api/layoutMenu'
export default {
  name: 'LayoutMenu',
  data () {
    return {
      layoutMenuList: [],
      addDialogVisible: false,
      addForm: {
        name: '',
        priority: 0,
        description: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          {
            min: 2,
            max: 6,
            message: '长度在 2 到 6 个字符',
            trigger: 'blur'
          }
        ],
        priority: [
          { required: true, message: '请输入优先级', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述', trigger: 'blur' },
          {
            min: 2,
            max: 30,
            message: '长度在 2 到 30 个字符',
            trigger: 'blur'
          }
        ]
      },
      updateDialogVisible: false,
      updateForm: {},
      updateFormRules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          {
            min: 2,
            max: 6,
            message: '长度在 2 到 6 个字符',
            trigger: 'blur'
          }
        ],
        priority: [
          { required: true, message: '请输入优先级', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述', trigger: 'blur' },
          {
            min: 2,
            max: 30,
            message: '长度在 2 到 30 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async getLayoutMenu () {
      const res = await getLayoutMenuData()
      this.layoutMenuList = res.data
    },
    addLayoutMenuShow () {
      this.addDialogVisible = true
    },
    addDialogclose () {
      this.addDialogVisible = false
      this.getLayoutMenu()
      this.$refs.addFormRef.resetFields()
    },
    async addDialogSubmit () {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return false
        const res = await addLayoutMenuData(this.addForm)
        this.$message.success(res.data)
        this.addDialogVisible = false
        this.getLayoutMenu()
      })
    },
    async updateGoodsTypeShow (id) {
      this.updateDialogVisible = true
      const res = await getLayoutMenuDataById(id)
      this.updateForm = res.data
    },
    updateDialogclose () {
      this.updateDialogVisible = false
    },
    updateDialogSubmit () {
      this.$refs.updateFormRef.validate(async (valid) => {
        if (!valid) return false
        const res = await updateLayoutMenuData(this.updateForm)
        this.$message.success(res.data)
        this.updateDialogVisible = false
        this.getLayoutMenu()
      })
    },
    async delGoodsTypeById (id) {
      const flag = await this.$confirm(
        '此操作将永久删除该首页菜单, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') {
        this.$message.info('取消删除首页菜单')
      } else {
        const res = await deleteLayoutMenuDataById(id)
        this.$message.success(res.data)
        this.getLayoutMenu()
      }
    }
  },
  created () {
    this.getLayoutMenu()
  }
}
</script>

<style>
.layoutMenu {
  margin-top: 20px;
}
</style>
